package com.zju.cst.cloudApp.server.encode;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;

public class MessageEncoder extends SimpleChannelHandler {

	@Override
	public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		
		String message = (String) e.getMessage();
		int length = message.length();
		
		ChannelBuffer buf = ChannelBuffers.buffer(length+4);
		buf.writeInt(length);
		buf.writeBytes(message.getBytes());
		
		Channels.write(ctx, e.getFuture(), buf);
	}
}	
